package com.ereport.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.ereport.entity.BudgetPlanner;
import com.ereport.util.ApplicationContext;

public class BudgetPlannerDAO {
	static Transaction tx;
	
	private  void endOperations(Session session) {
		
		session.close();
		session=null;
	}

	private  Session startOperations()throws Exception {
		
		Session session = ApplicationContext.getHiberSession().openSession();
		tx=session.beginTransaction();
		return session;
		
	}

	public int insert(BudgetPlanner bp) throws Exception {
		Session session = startOperations();
		session.saveOrUpdate(bp);
		session.getTransaction().commit();
		endOperations(session);
		return 0;
		
	}

	public List<BudgetPlanner> getBudget(int ex, String mnth, String year) throws Exception {
		List<BudgetPlanner> ob=new ArrayList<BudgetPlanner>();
		Session session = startOperations();
		String qrystr="From BudgetPlanner where exe_id=:id and budg_month=:m and budg_year=:y";
		Query qr=session.createQuery(qrystr);
		qr.setInteger("id", ex);
		qr.setString("m", mnth);
		qr.setString("y", year);
		ob=qr.list();
		session.getTransaction();
		endOperations(session);
		return ob;
		
	}

	public void deletBudget(int id, String mnth, String year) throws Exception {
		Session session=startOperations();
		String qtr="delete from BudgetPlanner where exe_id=:id and budg_month=:m and budg_year=:y";
		Query qry=session.createQuery(qtr);
		qry.setInteger("id", id);	
		qry.setString("m", mnth);	
		qry.setString("y", year);	
		qry.executeUpdate();
		endOperations(session);
		return ;
		
	}
}
